---- start of layout ----
Git is a distributed version control system that tracks changes in your code.
# Set global username and email
$ git config --global user.name "Your Name"
$ git config --global user.email "you@example.com"
# Check current configuration
$ git config --list
# Set default editor
$ git config --global core.editor "code --wait"
# Initialize a new repository
$ git init
# Clone an existing repository
$ git clone <repository-url>
# Check repository status
$ git status
# List branches
$ git branch
# Create a new branch
$ git branch <branch-name>
# Switch to a branch
$ git checkout <branch-name>
# Create and switch to a branch
$ git checkout -b <branch-name>
# Merge a branch into the current branch
$ git merge <branch-name>
# Delete a branch
$ git branch -d <branch-name>
# Stage files for commit
$ git add <file>
# Stage all files
$ git add .
# Commit changes
$ git commit -m "Commit message"
# Amend the last commit
$ git commit --amend
# Unstage files
$ git reset <file>
# Undo last commit (keep changes unstaged)
$ git reset --soft HEAD~1
# Undo last commit (discard changes)
$ git reset --hard HEAD~1
# Revert a specific commit
$ git revert <commit-hash>
# Add a remote
$ git remote add origin <url>
# List remotes
$ git remote -v
# Push changes to remote
$ git push origin <branch-name>
# Pull changes from remote
$ git pull origin <branch-name>
# Fork a repository (done on GitHub UI)
# Create a pull request (done on GitHub UI)
# Fetch and merge changes from the main branch
$ git fetch upstream
$ git merge upstream/main
# Rebase from main
$ git rebase main
Command | Description |
---|---|
git log | Show commit history |
git diff | Show changes between commits |
git stash | Temporarily save changes |
git stash pop | Apply stashed changes |
git tag | Create a tag for a specific commit |
git blame <file> | Show who last modified each line |
git show <commit-hash> | Show details of a specific commit |
git reflog | View the reference log of changes |
git cherry-pick <commit-hash> | Apply a specific commit to the current branch |
.gitignore
for files that don’t need to be tracked..gitignore
to exclude files from being tracked.Date: Nov 6, 2024
---- End of layout ----